<?php

namespace App\Http\Controllers\admin\courtyard;

use App\Http\Controllers\Controller;
use App\Logic\Es\EsService;
use App\Models\Courtyard;
use App\Service\QiNiuUpload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use phpseclib3\File\ASN1\Maps\PostalAddress;

class CourtyardareaController extends Controller
{
    /**
     * @后台院区管理展示页面
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/hospital/courtyard_area
     * @data
     * @return \Illuminate\Http\Response
     */
    public function courtyardArea(Request $request){
        $name=$request->get('name');
        //根据模型获取全部数据
        $data=Courtyard::where('name','like',"%$name%")->get();
        //页面渲染
        return view('hospital/courtyard_area',['data'=>$data,'name'=>$name]);
    }
    /**
     * @后台院区管理添加页面
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/hospital/courtyard_area
     * @data
     * @return \Illuminate\Http\Response
     */
    public function courtyardAdd(){
        //返回添加页面
        return view('hospital/courtyard_add');
    }


    /**
     * @后台院区管理添加
     * @user 刘璇
     * @method Post
     * @url https://www.healthcare.com/hospital/courtyard_insert
     * @data
     * @return \Illuminate\Http\Response
     */
    public function courtyardInsert(Request $request){
        try {
            //获取除了"_token"和"s"以外的其他数据并赋值
            $data=$request->except("_token","s");
//            //获取文件
//            $file=$request->file('img');
//            if ($file)
//            {
//                $file=Storage::disk('public')->put('img',$file);
//            }
//            $data['img']='/storage/' . $file;
            //七牛云上传
            $imagePath='';
            if ($request->file('img')){
                $imagePath=Storage::disk('public')->put(date('Ymd'),$request->file('img'));
//                dd($imagePath);
                $imageCloudPath=QiNiuUpload::upload('storage/'.$imagePath);
                $imagePath=$imageCloudPath;
            }
            $data['img']=$imagePath;
            //百度云审核
            $appId = "30392972";
            $apiKey = "TGOe2dNl7h4fpQsHNDA8apIH";
            $secretKey = "papPfKaeRCrQuBq6ZaSQKz5HVsARW48Y";
            $client = new \Luffy\TextCensor\Core($appId, $apiKey, $secretKey);
            $res = $client->textCensorUserDefined($request->name); //待审核文本字符串
            if($res['conclusion'] == "不合规") {
                return response()->json(['message' => '添加失败：内容不合规']);
            };
            //通过ORM模型进行添加
            $res=Courtyard::create($data);
            //返回
            return redirect("hospital/courtyard_area");
        }catch (Exception $exception){
            Log::info($exception->getMessage());
        }
    }



    /**
     * @后台院区管理修改页面
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/hospital/courtyard_update
     * @data
     * @return \Illuminate\Http\Response
     */
    public function courtyardUpdate(){
        //返回添加页面
        return view('hospital/courtyard_update');
    }














    /**
     * @后台院区管理删除
     * @user 刘璇
     * @method get
     * @url https://www.healthcare.com/hospital/courtyard_delete
     * @data
     * @return \Illuminate\Http\Response
     */
    public function courtyardDelete($id){
        $res=Courtyard::destroy($id);
        return redirect('hospital/courtyard_delete');
    }
}
